﻿var APP = APP || {};
APP.menuMap = function () {

    var map = [{
        id: "menu-home",
        children: [{
            id: "menu-home-newmatch"
        },
        {
            id: "menu-home-about"
        }]
    },
    {
        id: "menu-play-keyboard",
        children: [{
            id: "menu-play-main",
            children: [{
                id: "menu-play-help",
                children: [{
                    id: "menu-hint-result"
                }]
            },
            {
                id: "menu-quit-confirm"
            },
            {
                id: "menu-restart-confirm"
            },
            {
                id: "menu-play-match",
                children: [{
                    id: "menu-play-newmatch"
                }]
            }]
        }]
    }]

    var getParentId = function (id) {
        var parent = getParent(id);
        return parent ? parent.id : "";
    }

    var getParent = function (id) {
        var result = null;
        $.each(map, function (i, obj) {

            if (obj.id == id) {
                return false;
            }

            result = searchInChildren(obj, id);
            if (result)
                return false;
        });

        return result;
    }

    var searchInChildren = function (parentObj, id) {
        var result = null;

        if (parentObj.children && parentObj.children.length > 0) {
            $.each(parentObj.children, function (i, obj) {
                if (obj.id == id) {
                    result = parentObj;
                    return false;
                }

                result = searchInChildren(obj, id);
                if (result)
                    return false;
            });
        }

        return result;
    }

    return {
        getParent: getParent,
        getParentId: getParentId
    }
}